<?php

namespace App\Http\Controllers;

use App\Http\Model\Hint;
use App\Http\Services\DeviceServices;
use App\Modules\Popularize\Platform\PlatformFactory;
use App\Modules\Popularize\Services\PopularizeService;
use Illuminate\Http\Request;
use App\Lib\Response;
use Carbon\Carbon;

class DeviceController extends Controller
{
    protected $deviceServices;

    public function __construct(DeviceServices $deviceServices)
    {
        $this->deviceServices = $deviceServices;
    }

    public function test(Request $request)
    {
        if (empty($request["key"]) || $request["key"] != "HTfFfHr8CUMPT3cf") {
            die("参数错误");
        }

        $params = [];
        $type = $request["type"] ?? 0;
        switch ($type) {
            case 1: //激活
                echo "进入激活\n";
                $params["oaid"] = $request["oaid"];
                $instance = PlatformFactory::getInstance(['device' => $params]);
                if ($instance !== null) {
                    $service = new PopularizeService($instance);
                    $service->actviteEvent();
                }
                break;
            case 2: //注册
                echo "进入注册\n";
                $guid = $request["guid"];
                $instance = PlatformFactory::getInstance(['guid' => $guid]);
                if ($instance !== null) {
                    $service = new PopularizeService($instance);
                    $service->registerEvent();
                }
                break;
            case 3: //付费
                echo "进入付费\n";
                $user_id = $request["user_id"];
                $instance = PlatformFactory::getInstance(['user' => $user_id]);
                if ($instance !== null) {
                    $service = new PopularizeService($instance);
                    $service->payEvent(200);
                }
                break;
        }
        echo "操作完成\n";
    }

    /**
     * 存储渠道号
     */
    public function storagePlatform(Request $request)
    {
        $result = $this->deviceServices->savePlatform(['idfa' => $request->get("idfa") ?? ($request->get("imei") ?? ''), 'platform' => $request->get("platform")]);

        if (is_string($result))
            $result = json_decode($result, true);
        Response::returnResult($result['code'], $result['data']);
    }

    //广告归因，上报激活
    public function report(Request $request)
    {
        $data = $this->deviceServices->save($request->all());
        write_log('device', '设备激活上报', [$data]);
        if ($data['code'] != Hint::SUCCESS) {
            Response::returnResult(Hint::ERR_FAIL);
        }
        Response::returnResult(Hint::SUCCESS);
    }

    /**
     * 广告点击回调
     * */
    public function clackCallback(Request $request)
    {
        $params = $request->all();
        $data = $this->deviceServices->clackCallback($params);

        write_log('device', 'clackCallback', ['params'=>$params, 'result' => $data]);

        return json_encode($data, JSON_UNESCAPED_UNICODE);
    }

}